package book.manage;

import book.manage.entity.Book;
import book.manage.entity.Student;
import book.manage.mapper.BookMapper;
import book.manage.mapper.BorrowMapper;
import book.manage.mapper.StudentMapper;
import book.manage.utlis.MybatisUtil;
import lombok.extern.java.Log;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.Scanner;
import java.util.logging.LogManager;


@Log
public class Main {
    static SqlSession session = MybatisUtil.getSqlSession();
    static BorrowMapper borrowMapper = session.getMapper(BorrowMapper.class);
    static StudentMapper studentMapper = session.getMapper(StudentMapper.class);
    static BookMapper bookMapper = session.getMapper(BookMapper.class);


    public static void main(String[] args) throws IOException {

        try (Scanner scanner = new Scanner(System.in)) {
            LogManager manager = LogManager.getLogManager();
            manager.readConfiguration(Resources.getResourceAsStream("logging.properties"));

            while (true) {
                System.out.println("===============================");
                System.out.println("| 1. 录入学生信息  2. 录入书本信息 |");
                System.out.println("| 3. 添加借阅信息  4. 查询借阅信息 |");
                System.out.println("| 5. 查询学生信息  6. 查询书本信息 |");
                System.out.println("===============================");
                System.out.print("输入想要执行的操作(其他任意数字退出): ");
                int input;
                try {
                    input = scanner.nextInt();
                } catch (Exception e) {
                    return;
                }
                scanner.nextLine();
                switch (input) {
                    case 1:
                        addStudent(scanner);
                        break;
                    case 2:
                        addBook(scanner);
                        break;
                    case 3:
                        addBorrow(scanner);
                        break;
                    case 4:
                        showBorrow();
                        break;
                    case 5:
                        showStudent();
                        break;
                    case 6:
                        showBook();
                        break;
                    default:
                        return;
                }
            }
        }
    }

    private static void showBook() {
        bookMapper.getBookList().forEach(book -> {
            System.out.println(book.getBid() + "." + book.getTitle() + "[" + book.getPrice() + "] ( " + book.getDesc() + " )");
        });
    }


    private static void showStudent() {
        studentMapper.getStudentList().forEach(student -> {
            System.out.println(student.getSid() + "." + student.getName() + "[" + student.getSex() + "] ( " + student.getGrade() + " )");
        });
    }


    private static void showBorrow() {
        borrowMapper.getBorrow().forEach(borrow -> {
            System.out.println(borrow.getStudent().getName() + " -> " + borrow.getBook().getTitle());
        });
    }


    private static void addBorrow(Scanner scanner) {
        System.out.print("请输入学生号: ");
        String b = scanner.nextLine();
        int sid = Integer.parseInt(b);
        System.out.print("请输入书籍号: ");
        String a = scanner.nextLine();
        int bid = Integer.parseInt(a);

        int i = borrowMapper.addBorrow(sid, bid);
        if (i > 0) {
            System.out.println("书籍借阅录入成功~");
        } else {
            System.out.println("书籍借阅录入失败~ 请重试!");
        }

    }


    private static void addBook(Scanner scanner) {
        System.out.print("请输入书籍标题: ");
        String title = scanner.nextLine();
        System.out.print("请输入书籍介绍: ");
        String desc = scanner.nextLine();
        System.out.print("请输入书籍价格: ");
        String price = scanner.nextLine();
        double p = Double.parseDouble(price);

        Book book = new Book(title, desc, p);

        int i = bookMapper.addBook(book);
        if (i > 0) {
            System.out.println("书籍信息录入成功~");
            log.info("新增一条书籍信息: " + book);
        } else {
            System.out.println("书籍信息录入失败~ 请重试!");
        }
    }


    private static void addStudent(Scanner scanner) {
        System.out.print("请输入学生名字: ");
        String name = scanner.nextLine();
        System.out.print("请输入学生性别(1. 男 / 2. 女): ");
        String sex = scanner.nextLine();
        System.out.print("请输入学生年级: ");
        String grade = scanner.nextLine();
        int g = Integer.parseInt(grade);

        Student student = new Student(name, sex, g);

        int i = studentMapper.addStudent(student);
        if (i > 0) {
            System.out.println("学生信息录入成功~");
            log.info("新增一条学生信息: " + student);
        } else {
            System.out.println("学生信息录入失败~ 请重试!");
        }
    }
}
